package com.zhoujian.Prototype.DeepCopy;

import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
public class PromotionEvent implements Cloneable, Serializable {
    private String name;
    private Date startDate;
    private Date endDate;
    private List<PromotionRule> rules;

    //省略构造函数，和getter和setter方法

    //在促销活动中的clone方法需要克隆里面所有的非基础数据类型
    @Override
    public PromotionEvent clone() {
        try {
            PromotionEvent cloneEvent = (PromotionEvent) super.clone();
            if (startDate != null) {
                cloneEvent.startDate = (Date) startDate.clone();
            }
            if (endDate != null) {
                cloneEvent.endDate = (Date) endDate.clone();
            }
            if (rules != null && rules.size() > 0) {
                List<PromotionRule> copyRules = new ArrayList<>();
                for (PromotionRule rule : rules) {
                    copyRules.add((PromotionRule) rule.clone());
                }
                cloneEvent.setRules(copyRules);
            }
            return cloneEvent;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }
}
